import {defineStore} from "pinia";
import {ref} from "vue";
import {
    getTestCaseService,
    removeTestCaseService,
    saveTestCaseService,
    updateTestCaseService
} from "@/api/server/test-case.ts";

export const useTestCaseStore = defineStore('testCase', () => {
    const problemId = ref('')
    const testCase = ref('')

    function resetTestCase() {
        problemId.value = ''
        testCase.value = ''
    }

    async function initTestCase(newProblemId: string) {
        problemId.value = newProblemId
        const TestCase = await getTestCaseService(newProblemId) as any
        testCase.value = TestCase.testCase
    }

    async function saveTestCase(newProblemId: string, newTestCase: string) {
        await saveTestCaseService(newProblemId, newTestCase)
        problemId.value = newProblemId
        testCase.value = newTestCase
    }

    async function updateTestCase(newProblemId: string, newTestCase: string) {
        await updateTestCaseService(newProblemId, newTestCase)
        problemId.value = newProblemId
        testCase.value = newTestCase
    }

    async function removeTestCase(newProblemId: string) {
        await removeTestCaseService(newProblemId)
        resetTestCase()
    }

    return {
        problemId,
        testCase,
        resetTestCase,
        initTestCase,
        saveTestCase,
        updateTestCase,
        removeTestCase
    }
});